package com.ygxsk.carhome.ui.rice;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.constant.ConstantSign;
import com.ygxsk.carhome.response.SmartGoOutItemBean;
import com.ygxsk.carhome.ui.BaseImmersionFragment;
import com.ygxsk.carhome.ui.adapter.SmartGoItemAdapter;
import com.ygxsk.carhome.utils.CommUtils;
import com.ygxsk.carhome.views.ClearEditTextNoClearSK;
import com.ygxsk.carhome.views.MyLinearLayoutManager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.internal.DebouncingOnClickListener;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RiceTicketRecordHistoryFragment#newInstance} factory method to
 * create an instance of this fragment. 是携带 智能出门的核销或者未核销的记录的
 * <p>
 * 废弃
 */
public class RiceTicketRecordHistoryFragment extends BaseImmersionFragment {

    @BindView(R.id.et_searchincustom)
    ClearEditTextNoClearSK et_searchInCustom;
    //    点击的【搜索】按钮内容
    @BindView(R.id.textviewsearchincustom)
    TextView textViewSearchInCustom;

    @BindView(R.id.refreshlayoutinrecordhistory)
    SmartRefreshLayout refreshLayoutInRecordHistory;

    @BindView(R.id.recyclerviewinrecordhistory)
    RecyclerView recyclerViewInRecordHistory;

    //上传搜索的关键字的手机号信息
    private String keyWord_phone_upLoad;

    private ArrayList<SmartGoOutItemBean> mDatasResources = new ArrayList<>();

    //添加
    private SmartGoItemAdapter smartGoItemAdapter;


    public RiceTicketRecordHistoryFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment SmartGoRecordHistoryFragment.
     */
//    public static SmartGoRecordHistoryFragment newInstance(String param1, String param2) {
    public static RiceTicketRecordHistoryFragment newInstance() {
        RiceTicketRecordHistoryFragment fragment = new RiceTicketRecordHistoryFragment();
        Bundle args = new Bundle();
//        args.putString(ARG_PARAM1, param1);
//        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mFragmentInstance = this;
        mActivityInstance = this.getActivity();

        if (getArguments() != null) {
//            mParam1 = getArguments().getString(ARG_PARAM1);
//            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        //接收外界刷新消息
//        if (!EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().register(this);
//        }

    }

    @Override
    protected void initView() {
        super.initView();

        //勿删，将来自动计算时候放开 2023-6-17 17:47:13
        try {

            //do
            if (et_searchInCustom != null) {

                et_searchInCustom.setHint(StringUtils.getString(R.string.communitysearchhint));
                keyWord_phone_upLoad = et_searchInCustom.getText().toString().trim();
                //默认进入页面是清掉焦点的
                et_searchInCustom.clearFocus();

                //软键盘的【搜索】
                et_searchInCustom.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                        if (actionId == EditorInfo.IME_ACTION_SEARCH || (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                            //关闭软键盘
                            if (KeyboardUtils.isSoftInputVisible(mFragmentInstance.getActivity())) {
                                KeyboardUtils.hideSoftInput(mFragmentInstance.getActivity());
                            }
                            method_SearchSomeThing();
                            return true;
                        }
                        return false;
                    }
                });

            }

            if (textViewSearchInCustom != null) {

                textViewSearchInCustom.setOnClickListener(new DebouncingOnClickListener() {
                    @Override
                    public void doClick(View v) {
                        if (KeyboardUtils.isSoftInputVisible(mFragmentInstance.getActivity())) {
                            KeyboardUtils.hideSoftInput(mFragmentInstance.getActivity());
                        }
                        method_SearchSomeThing();
                    }
                });
            }

            //适配器的内容
            smartGoItemAdapter = new SmartGoItemAdapter(//
                    R.layout.smartgolistadapter, //和外侧列表是一个适配器；
                    mDatasResources);//标识是银行客户信息

            if (isAdded()) {
                recyclerViewInRecordHistory.setLayoutManager(new MyLinearLayoutManager(this.getContext()));
            } else {
                recyclerViewInRecordHistory.setLayoutManager(new MyLinearLayoutManager(this.getActivity()));
            }

            recyclerViewInRecordHistory.clearAnimation();
            recyclerViewInRecordHistory.setAdapter(smartGoItemAdapter);
            smartGoItemAdapter.notifyDataSetChanged();

            // 2023-04-09 中间是否有详情？？？
            refreshLayoutInRecordHistory.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {

                @Override
                public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                LogUtils.d(ConstantSign.LOGCAT_NOMAL + "refreshLayoutInBankCustomerRunMan---onRefresh");
                    refreshLayout.getLayout().post(new Runnable() {
                        @Override
                        public void run() {

                            refreshLayout.finishRefresh();
                        }
                    });
                }

                @Override
                public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

                    refreshLayout.getLayout().post(new Runnable() {

                        @Override
                        public void run() {

                        }
                    });
                }
            });

        } catch (Exception e) {
            //catch
            e.printStackTrace();
        }

    }

    private void method_SearchSomeThing() {

        //获取搜索的关键字内容,刚开始默认是空值
        keyWord_phone_upLoad = et_searchInCustom.getText().toString().trim();

//        if (TextUtils.isEmpty(et_searchInCustom.getText().toString().trim())) {
//            //ToastUtils.make().setGravity(Gravity.CENTER, 0, 0);
//            //ToastUtils.showShort("请输入搜索关键字");
//            CommUtils.checkDialog(alertViewDialogSure);
//            alertViewDialogSure = CommUtils.method_showAlertViewSingleSlowly(mActivityInstance, ConstantSign.ALERT_TITLE, StringUtils.getString(R.string.pleaseinputkeyword), true);
//            alertViewDialogSure.show();
//            //不发生搜索
//            return;
//        }
//
        if (!TextUtils.isEmpty(et_searchInCustom.getText().toString().trim()) && (et_searchInCustom.getText().toString().trim()).length() < 2) {
            //ToastUtils.showShort("最少输入两个字符！");
            CommUtils.checkDialog(mAlertView);
            mAlertView = CommUtils.method_showAlertViewSingleSlowly(mActivityInstance, ConstantSign.ALERT_TITLE, StringUtils.getString(R.string.pleaseinputkeywordtwo), true);
            mAlertView.show();
            //不发生搜索
            return;
        }

//        //如果发起搜索，要重置 第一页
//        pageNum_upLoad = Parameterkey.pageNum_default_first;
//
//        if (mDatasResources != null && !mDatasResources.isEmpty()) {
//            mDatasResources.clear();
//        }
//
//        if (runManBankCustomerInfoAdapter != null) {
//            runManBankCustomerInfoAdapter.notifyDataSetChanged();
//        }
//
//        isShowLoading = true;
//
//        getServerDatas();
//
//        if (TextUtils.isEmpty(keyWord_name_upLoad) || TextUtils.isEmpty(keyWord_phone_upLoad)) {
//            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0);
//            ToastUtils.showShort("请输入搜索关键字");
//        }

    }

    @Override
    protected void gainDatas() {

        //

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_smart_go_record_history;
    }

    @Override
    public void onClickNodata(View view) {

        //

    }
}